package com.example.ggpicturegg.test;

import com.example.ggpicturegg.Mysql_com.DbOpenHelper;
import com.mysql.jdbc.PreparedStatement;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;

public class PathDao extends DbOpenHelper {

    String contextPath;
    public PathDao() {
    }

    public void SelectPathInsertIntoDB(int id,String path) {

        System.out.println(path);

        try {
            getConnection();
            String insertsql = "insert into tb_Path(id,path) value(?,?)";
            pStmt = (PreparedStatement) conn.prepareStatement(insertsql);
            pStmt.setInt(1, id);
            pStmt.setString(2, path);
            int iRow = pStmt.executeUpdate();
            System.out.println("文件路径成功插入数据库");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public void ReadPathFromDB(int id){

        try {
            getConnection();
            String selectsql="select path from tb_Path where id=?";
            pStmt= (PreparedStatement) conn.prepareStatement(selectsql);
            pStmt.setInt(1,id);
            rs  = pStmt.executeQuery();
            while(rs.next()){
                contextPath = rs.getString("path");
                System.out.println(contextPath);
            }
            File f = new File(contextPath);
            //使用FileReader读取文件内容出现乱码
//          FileReader fr=new FileReader(f);
//          while(fr.read()!=-1){
//              char ch=(char) fr.read();
//              System.out.print(ch);
//          }
//          fr.close();
//
            FileInputStream fi=new FileInputStream(f);
            BufferedReader br=null;
            br=new BufferedReader(new InputStreamReader( fi,"UTF-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}